from typing import Optional, List
import os

from pydantic_settings import BaseSettings
from pydantic import ConfigDict

# 知识库配置
KNOWLEDGE_BASE_CONFIG = {
    "vector_db": {
        "type": "milvus",  # 或 "pinecone", "weaviate"
        "host": "localhost",
        "port": 19530,
        "collection_name": "knowledge_base",
        "dimension": 1536,
        "metric_type": "COSINE"
    },
    "embedding": {
        "model": "text-embedding-ada-002",
        "chunk_size": 1000,
        "chunk_overlap": 200
    },
    "retrieval": {
        "top_k": 10,
        "score_threshold": 0.7,
        "rerank_top_k": 5
    }
}


class Settings(BaseSettings):

    # 应用配置
    APP_NAME: str = "YxYServer"
    APP_VERSION: str = "1.0.0.0"
    
    # 数据库配置 - 生产环境配置（用户需要修改这些值）
    DB_USER: str = "yxy"
    DB_PASSWORD: str = "7ZZb8L!F"  # 用户需要修改
    DB_HOST: str = "172.16.21.77"       # 用户需要修改
    DB_PORT: str = "4040"
    DB_NAME: str = "yxy"
    
    @property
    def DATABASE_URL(self) -> str:
        """获取数据库连接URL"""
        return f"mysql+aiomysql://{self.DB_USER}:{self.DB_PASSWORD}@{self.DB_HOST}:{self.DB_PORT}/{self.DB_NAME}?charset=utf8mb4&autocommit=false&use_unicode=true&sql_mode=STRICT_TRANS_TABLES&init_command=SET SESSION sql_mode='STRICT_TRANS_TABLES'"

    # 仓库配置
    REPOSITORY_BASE_DIR: str = os.path.join(os.path.dirname(os.path.dirname(__file__)), "repositories")

    # AI 大模型配置
    AI_PROVIDER: str = "zhipu"  # 使用智谱LLM

    # AI API配置 - 生产环境配置（用户需要修改这些值）
    AI_AUTH_URL: str = "http://172.16.21.142:7861/token"
    AI_CHAT_URL: str = "http://116.142.76.181:20025/v1/chat/completions"
    AI_YXY_KNOWLEDGE_URL: str = "http://172.16.21.142:7861/chat/knowledge_base_chat"
    AI_YXY_CHAT_URL: str = "http://172.16.21.142:7861/chat/chat"  # 普通聊天接口
    AI_KNOWLEDGE_URL: str = "http://172.16.21.142:7861/knowledge_base/search_docs"
    AI_USERNAME: str = "dxd"           # 用户需要修改
    AI_PASSWORD: str = "123"           # 用户需要修改
    AI_MODEL_NAME: str = "deepseek-llm-32B-chat"
    AI_TEMPERATURE: float = 0.7
    AI_MAX_TOKENS: int = 0
    AI_PROMPT_NAME: str = "default"
    AI_API_URL: str = "http://localhost:8001/scan"
    AI_API_KEY: str = "your_api_key_here"
    AI_STREAM_ENABLED: str = "true"

    # 讯飞 API配置
    XUNFEI_API_KEY: str = "Bearer MYdbqgtXFalipqVYfxWs:qpuJTZmXXCEjfSFgEaOx"
    XUNFEI_CHAT_URL: str = "https://spark-api-open.xf-yun.com/v2/chat/completions"

    # 阿里千问 API配置
    ALI_QW_API_KEY: str = "sk-743a91ddea7346b39d993c6130340e67"
    ALI_QW_CHAT_URL: str = "https://dashscope.aliyuncs.com/compatible-mode/v1"
    ALI_QW_MODEL: str = "qwen-turbo-latest"

    # 智谱 API配置
    ZHIPU_API_KEY: str = "12a39955a206475b89007dc5b0de9ed8.Gq7dHQSAwhYF8Kf9"
    ZHIPU_CHAT_URL: str = "https://open.bigmodel.cn/api/paas/v4/chat/completions"
    ZHIPU_MODEL: str = "glm-4-flash-250414"

    # 文件分片大小 智谱支持200万字
    CHUNK_SIZE: int = 10000 * 200
    # 关联数量
    TOP_K: int = 5
    # 分数阈值
    SCORE_THRESHOLD: float = 0.8
    # 最大token数
    MAX_TOKENS: int = 16384;
    TEMPERATURE: float= 0.95



    # 应用配置
    DEBUG: bool = False  # 生产环境设为False

    # 扫描配置
    SCAN_TIMEOUT: int = 300  # 扫描超时时间（秒）
    MAX_CONCURRENT_SCANS: int = 15
    C_FILE_EXTENSIONS: List[str] = [".c", ".cpp", ".h", ".hpp"]  # C/C++ 文件扩展名

    # 临时目录配置
    TEMP_DIR: str = "/tmp/yxyweb"
    TEMP_FILE_RETENTION_MINUTES: int = 30
    
    # 文件上传配置
    UPLOAD_DIR: str = "/uploads"  # 文件上传目录
    MAX_FILE_SIZE: int = 50 * 1024 * 1024  # 最大文件大小 (50MB)
    ALLOWED_FILE_TYPES: List[str] = [".docx", ".pdf", ".txt", ".md"]  # 允许的文件类型

    # 扫描配置
    SCAN_BATCH_SIZE: int = 10  # 每批扫描的文件数
    SCAN_RETRY_COUNT: int = 3  # 扫描失败重试次数
    SCAN_RETRY_DELAY: int = 5  # 重试间隔（秒）

    model_config = ConfigDict(
        env_file=".env",
        extra="allow"  # 允许额外字段
    )


settings = Settings()
